package MTRequest;

import android.database.Cursor;
import MTClient.ClientLogic;
import MTClient.MTConstant;
import MTClient.MessageCentre;
import SQLManager.SQLManager;

public class MTClientSynContactList extends MTRequest{
	
	private int curIndex = 0;

	public MTClientSynContactList( MessageCentre aMSGCenter )
	{
		super( aMSGCenter );
		this.mRequestState = MTConstant.MT_REQ_CSYN_CTL_CHECK_PHONE_CONTACT;
	}
	
	protected void UpdateRequestState( int aPeerState )
	{
		if( aPeerState == MTConstant.MT_REQ_SSYN_CTL_SEVRER_READY ){
			this.mRequestState = MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM;
		}
		else{
			++curIndex;
			if( curIndex == this.mSendingToPeerRawData.size() - 1 ){
				this.mRequestState = MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM_LAST;
			}
			else if( curIndex >= this.mSendingToPeerRawData.size() ){
				this.mRequestState = MTConstant.MT_REQ_FINISH;
			}
		}
	}
	
	protected void ProcessReqest( int aPeerState, String peerRawData )
	{
		switch( aPeerState ){
			case MTConstant.MT_REQ_SSYN_CTL_SEVRER_READY:{
				break;
			}
			case MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM:
			case MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM_LAST:{
				
				// Get User ID
				if( peerRawData.matches(" ") ){
					// No this phone number
				}else{
					// Update sever
					HandleUpdateContactList(peerRawData);
				}
				
				System.out.println("Client UserID:" + peerRawData );
				
				//
				// update sql...
				//
				this.mReceivedFromPeerRawData.add( peerRawData );

				
				break;
			}
		}
	}
	
	//
	public void SendRequest()
	{
		SynPhoneContactList();
		
		String request = "" + MTConstant.MT_REQID_SYNCHRONIZE_CONTACT_LIST + MTConstant.MT_REQ_ITEM_SEPARATER + 
		this.mRequestState + MTConstant.MT_REQ_ITEM_SEPARATER;
		switch( this.mRequestState ){
			case MTConstant.MT_REQ_CSYN_CTL_SEND_USERID:{
				// 
				//  Get UserID 
				//
				// ....
				//
				String userID = this.mRequestDataSource.GetRawData();
				request += userID;
				break;
			}
			case MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM:
			case MTConstant.MT_REQ_CSYN_CTL_SEND_PHONENUM_LAST:{
				String phoneNum = this.mSendingToPeerRawData.get( curIndex );
				request += phoneNum;
				break;
			}
			case MTConstant.MT_REQ_FINISH:{
				ClientLogic.GetInstance().Notify(MTConstant.MT_REQ_SSYN_CTL_REPLY_PHONENUM_LAST, 0, null);
				this.Finish();
				return;
			}
		}
		this.SendMSG(request);
	}
	
	
	private void SynPhoneContactList()
	{
		if( MTConstant.MT_REQ_CSYN_CTL_CHECK_PHONE_CONTACT == this.mRequestState ){
			System.out.println( "-->SynPhoneContactList()");
			//
			// if no local phone contact table
			//
			//  ......
			//
			SQLManager sqlManager = SQLManager.GetInstance();
			sqlManager.open();
			Cursor contactListWithourUserID = sqlManager.FetchAllContactListWithoutUserID();
			
			sqlManager.close();
			
			//Remark: Prepare all sending phone number without UserID
			System.out.println( "contactListWithourUserID.getCount:" + contactListWithourUserID.getCount());
			if( contactListWithourUserID.getCount() > 0 ){
				while( contactListWithourUserID.moveToNext() ){
					this.mSendingToPeerRawData.add( 
							contactListWithourUserID.getString( 
									contactListWithourUserID.getColumnIndex( SQLManager.KEY_LCLTB_PHONENUMBER ) ));
				}

				//Synchronize the Local Contact List Table.
				
				this.mRequestState = MTConstant.MT_REQ_CSYN_CTL_SEND_USERID;
			}
			else{
				// No Update
				this.mRequestState = MTConstant.MT_REQ_FINISH;
			}
			
		}
	}
	
	private void HandleUpdateContactList( String aUserID )
	{
		SQLManager sqlManager = SQLManager.GetInstance();
		sqlManager.open();
		sqlManager.UpdateContactListMember( aUserID, this.mSendingToPeerRawData.get( curIndex ) );
		sqlManager.close();
	}
}
